package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.MessageMapper;
import com.haiyu.manager.dto.BoardDTO;
import com.haiyu.manager.pojo.Message;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl  implements MessageService {

    @Autowired
    MessageMapper messageMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public PageDataResult getMessageList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<Message> messages = messageMapper.getMessageList();

        System.out.println("messages有" + messages);
        PageHelper.startPage(pageNum, pageSize);

        if (messages.size() != 0) {
            PageInfo<Message> pageInfo = new PageInfo<>(messages);
            pageDataResult.setList(messages);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> updateMessage(Message message) {
        Map<String,Object> data = new HashMap();

        int result = messageMapper.updateMessage(message);
        if(result == 0){
            data.put("code",0);
            data.put("msg","更新失败！");
            logger.error("message[更新]，结果=更新失败！");
            return data;
        }
        data.put("code",1);
        data.put("msg","更新成功！");
        logger.info("message[更新]，结果=更新成功！");
        return data;
    }

    @Override
    public Map<String, Object> delMessage(Integer messageId, Integer textStatus) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除
            int result = messageMapper.updateMessageStatus(messageId,textStatus);
            if(result == 0){
                data.put("code",0);
                data.put("msg","操作失败");
                logger.error("操作失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","操作成功");
            logger.info("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("操作异常！", e);
        }
        return data;
    }

    @Override
    public Message getMessageById(Integer messageId) {
        return messageMapper.selectByPrimaryKey(messageId);
    }

    @Override
    public Map<String, Object> recoverMessage(Integer messageId, Integer textStatus) {
        Map<String, Object> data = new HashMap<>();
        try {
            int result = messageMapper.updateMessageStatus(messageId,textStatus);
            if(result == 0){
                data.put("code",0);
                data.put("msg","操作失败");
            }
            data.put("code",1);
            data.put("msg","操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("操作异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> addMessage(Message message) {
        Map<String,Object> data = new HashMap();
        try {

            message.setTextStatus(2);
            int result = messageMapper.insertMessage(message);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("公告[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("公告[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("公告[新增]异常！", e);
            return data;
        }
        return data;
    }
}
